package com.ipartek.formacion.migracion.dao;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.ipartek.formacion.migracion.beans.Persona;
import com.ipartek.formacion.migracion.ddbb.TxtDaoFactory;
import com.ipartek.formacion.migracion.excepciones.PersonaException;
import com.ipartek.formacion.migracion.interfaces.InterfacePersonaDao;

public class TxtPersonaDao implements InterfacePersonaDao {

	private static final Logger log = Logger.getLogger(TxtPersonaDao.class);

	public TxtPersonaDao() {
		PropertyConfigurator.configure("config/log4j.properties");
	}

	@Override
	public boolean anadirPersona(Persona persona) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public ArrayList<Persona> getPersonas() {
		ArrayList<Persona> lista = new ArrayList<Persona>();
		try {
			BufferedReader br = new BufferedReader(new FileReader(
					TxtDaoFactory.conectar()));
			String linea[];

			while ((linea = br.readLine().split(",")) != null) {
				try {
					// 0. String nombre
					// 1. String apellido1
					// 2. String apellido2
					// 3. Integer edad
					// 4. String eMail
					// 5. String dni
					// 6. String rol
					Persona p = new Persona(linea[0], linea[1], linea[2],
							Integer.parseInt(linea[3]), linea[4], linea[5],
							linea[6]);

					lista.add(p);
					p = null;
				} catch (PersonaException e) {
					log.error(e.getMensajeError());
					// } catch (NumberFormatException e) {
					// log.error(e.getMessage());
				} catch (Exception e) {
					log.error(e.getMessage());
				} finally {
					continue;
				}
			}
		} catch (FileNotFoundException e) {
			// TODO log
			e.printStackTrace();
		} catch (IOException e) {
			// TODO log
			e.printStackTrace();
		} finally {
			return lista;
		}
	}

	@Override
	public void comitar() {
		// TODO Auto-generated method stub

	}

	@Override
	public void conectar() {
		// TODO Auto-generated method stub

	}
}
